package com.ww.springboot.springstudy.config;

import org.springframework.context.annotation.DeferredImportSelector;
import org.springframework.core.type.AnnotationMetadata;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.function.Predicate;

/**
 * 描述：可以在AutoConfigurationImportSelector上扩展 也可以自己继承DeferredImportSelector并重写逻辑
 *
 * @author 🧑 ‍wanwei
 * @since 2023-03-15 17:01
 */
public class MyDeferredImportSelector implements DeferredImportSelector {

    @Override
    public String[] selectImports(AnnotationMetadata importingClassMetadata) {
        return new String[0];
    }

    @Override
    public Class<? extends Group> getImportGroup() {
        return MyAutoConfigurationGroup.class;
    }

    @Override
    public Predicate<String> getExclusionFilter() {
        return name -> {
            System.out.println(name);
            return true;
        };
    }


    /**
     * group 可以有多个 且互相不影响 最终会汇总
     */
    private static class MyAutoConfigurationGroup
            implements DeferredImportSelector.Group {
        @Override
        public void process(AnnotationMetadata metadata, DeferredImportSelector selector) {
            //获取注解参数 及对象本身
            System.out.println(metadata);
            System.out.println(selector);
        }

        @Override
        public Iterable<Entry> selectImports() {
            //返回可用的import
            return new ArrayList<>();
        }
    }
}
